package fuckjava;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;

import net.sf.json.JSONObject;

public class json {
	private static Gson gson = new Gson();
	public static void main(String[] args) {
		String s  = " ";
		System.out.println(isJson(s));
		//JSONObject json0 = JSONObject.fromObject(s);
		//System.out.println(json0);
		
		List<String> list = new ArrayList<String>();
		for(int i = 0; i < list.size(); i++) {
			System.out.println(list.get(0));
		}
		
		JSONObject json = new JSONObject();
		JSONObject ctrlJson = null;
		Object ctrlObj = json.get("ctrl");
		System.out.println("22222"+ctrlObj+"2222");
		if(null!=ctrlObj) {
			//ctrlJson = JSONObject.fromObject(ctrlObj);
			//System.out.println(ctrlJson);
			System.out.println(ctrlJson);
		}
	}
	
	
	
	
	
	
	/**
	 * 判断传入的字符串是否为完整正确的json格式
	 * @param jsonStr
	 * @return
	 */
	public static  boolean isJson(String jsonStr){
		if(null==jsonStr||StringUtils.isBlank(jsonStr)) {
			return false;  
		}
		try {
			fromJson(jsonStr, JSONObject.class);
			System.out.println("11"+fromJson(jsonStr, JSONObject.class));
			return  true;   
		} catch (Exception e) { 
			return false;  
		}
	}
	
	/**
	 * json转换成对象
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T fromJson(String json, Class<T> clazz) {
		if (json == null || StringUtils.isBlank(json)) {
            return null;
        }
		
		return gson.fromJson(json, clazz);
	}
}
